/*
 * Copyright 2020-2030, MateCloud, DAOTIANDI Technology Inc All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * Author: pangu(7333791@qq.com)
 */
package com.yc.cloud.admin.controller;

import com.yc.cloud.admin.service.UmsRoleService;
import com.yc.cloud.common.basic.api.ResponseResult;
import com.yc.cloud.common.basic.dto.BaseQueryParam;
import com.yc.cloud.common.orm.CommonPage;
import com.yc.cloud.entity.UmsRole;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import jakarta.annotation.Resource;
import lombok.AllArgsConstructor;
import lombok.val;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 后台用户角色表 前端控制器
 * </p>
 *
 * @author JackGu
 * @since 2022-09-11
 */
@RestController
@AllArgsConstructor
@RequestMapping("/role")
@Api(value = "后台用户角色表", tags = "后台用户角色表接口")
public class UmsRoleController {

    @Resource
    private final UmsRoleService umsRoleService;


    @GetMapping("/list")
    @ApiOperation(value = "列表", notes = "支持分页查询,字段模糊匹配,排序等")
    public ResponseResult<CommonPage<UmsRole>> list(BaseQueryParam param) {
        val result = umsRoleService.list(param);
        return ResponseResult.success(CommonPage.restPage(result));
    }


    @GetMapping("/{id}")
    @ApiOperation(value = "详情", notes = "根据ID查询")
    public ResponseResult<UmsRole> getItem(@PathVariable @ApiParam(value = "ID", required = true)  Long id) {
        return ResponseResult.success(umsRoleService.getById(id));
    }


    @PostMapping("/create")
    @ApiOperation(value = "添加", notes = "添加后台用户角色表")
    public ResponseResult<UmsRole> create(@Validated @RequestBody UmsRole umsRole) {
        val result = umsRoleService.save(umsRole);
        if (result) {
            return ResponseResult.success(umsRole);
        }
        return ResponseResult.failed();
    }


    @ApiOperation(value = "删除", notes = "通过ID删除")
    @PostMapping(value = "/delete/{id}")
    @ResponseBody
    public ResponseResult<Long> delete(@PathVariable @ApiParam(value = "ID", required = true) Long id) {
        val result = umsRoleService.removeById(id);
        if (result) {
            return ResponseResult.success();
        }
        return ResponseResult.failed();
    }


    @PostMapping("/delete/batch")
    @ApiOperation(value = "批量删除", notes = "通过id逗号分隔,批量删除")
    public ResponseResult<List<Long>> delete(@RequestParam List<Long> ids) {
        val result = umsRoleService.removeByIds(ids);
        if (result) {
            return ResponseResult.success();
        }
        return ResponseResult.failed();
    }


    @ApiOperation(value = "修改", notes = "通过ID修改")
    @PostMapping(value = "/update/{id}")
    @ResponseBody
    public ResponseResult<Long> update(@PathVariable @ApiParam(value = "ID", required = true) Long id, @Validated @RequestBody UmsRole umsRole) {
        umsRole.setId(id);
        val result = umsRoleService.updateById(umsRole);
        if (result) {
            return ResponseResult.success();
        }
        return ResponseResult.failed();
    }

    @ApiOperation("给角色分配菜单")
    @RequestMapping(value = "/allocMenu", method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult<Integer> allocMenu(@RequestParam Long roleId, @RequestParam List<Long> menuIds) {
        int count = umsRoleService.allocMenu(roleId, menuIds);
        return ResponseResult.success(count);
    }

    @ApiOperation("给角色分配资源")
    @RequestMapping(value = "/allocResource", method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult<Integer> allocResource(@RequestParam Long roleId, @RequestParam List<Long> resourceIds) {
        int count = umsRoleService.allocResource(roleId, resourceIds);
        return ResponseResult.success(count);
    }

}
